package app.server.project.core.threadpool;

import app.server.project.core.constants.Def;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

@Configuration
@Slf4j
public class ThreadPool {

    /**
     * 配置线程池，通用线程池，随机，无序
     *
     * @return
     */
    @Bean(Def.CommonPool)
    public ThreadPoolTaskScheduler taskScheduler() {
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        scheduler.setPoolSize(Runtime.getRuntime().availableProcessors() * 2);
        scheduler.setThreadNamePrefix("app-pool-");
        scheduler.setErrorHandler(c -> {
            log.error("=====线程池发生错误====", c);
        });
        scheduler.setRejectedExecutionHandler((r, c) -> {
            log.error("=======线程池发生拒绝====", r);
        });
        scheduler.initialize();
        return scheduler;
    }
}
